//
//  HomePageViewController.m
//  CommunitySupermarketProiect
//
//  Created by 吕惠卿 on 2017/9/25.
//  Copyright © 2017年 吕惠卿. All rights reserved.
//
#define NAVBAR_COLORCHANGE_POINT (-IMAGE_HEIGHT + NAV_HEIGHT)
#define NAV_HEIGHT 64
#define IMAGE_HEIGHT 200
#define SCROLL_DOWN_LIMIT 70
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define LIMIT_OFFSET_Y -(IMAGE_HEIGHT + SCROLL_DOWN_LIMIT)


#import "HomePageViewController.h"

#import "SDCycleScrollView.h"
#import "WRImageHelper.h"
#import "WRNavigationBar.h"
@interface HomePageViewController ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) SDCycleScrollView *advView;



@end


static NSString *homefirstColl = @"homefirstColl";
static NSString *homesecondColl = @"homesecondColl";
static NSString *homethirdColl = @"homethirdColl";
static NSString *homefourColl = @"homefourColl";
static NSString *homefiveColl = @"homefiveColl";
static NSString *homesixColl = @"homesixColl";
static NSString *homesevenColl = @"homesevenColl";
static NSString *homeeightColl = @"homeeightColl";
static NSString *homenineColl = @"homenineColl";
static NSString *LineGrayColl = @"LineGrayColl";

@implementation HomePageViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    WhiteColor;
    [self setupNavItems];
    self.collectionView.contentInset = UIEdgeInsetsMake(IMAGE_HEIGHT-64, 0, 0, 0);
    [self.collectionView addSubview:self.advView];
    [self.view addSubview:self.collectionView];
    
    [self wr_setNavBarBarTintColor:[UIColor colorWithRed:28/255.0 green:178/255.0 blue:140/255.0 alpha:1.0]];
    [self wr_setNavBarBackgroundAlpha:0];
    
}

- (void)setupNavItems
{
    self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, -3, 230, 30)];
    [self.searchButton setTitle:@"搜索 商家" forState:UIControlStateNormal];
    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [self.searchButton addTarget:self action:@selector(onClickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = self.searchButton;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < -IMAGE_HEIGHT) {
        [self updateNavBarButtonItemsAlphaAnimated:.0f];
    } else {
        [self updateNavBarButtonItemsAlphaAnimated:1.0f];
    }
    
    if (offsetY > NAVBAR_COLORCHANGE_POINT)
    {
        CGFloat alpha = (offsetY - NAVBAR_COLORCHANGE_POINT) / NAV_HEIGHT;
        [self wr_setNavBarBackgroundAlpha:alpha];
        [self updateSearchBarColor:alpha];
    }
    else
    {
        [self wr_setNavBarBackgroundAlpha:0];
        [self.searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    
    //限制下拉的距离
    if(offsetY < LIMIT_OFFSET_Y) {
        [scrollView setContentOffset:CGPointMake(0, LIMIT_OFFSET_Y)];
    }
}

- (void)updateNavBarButtonItemsAlphaAnimated:(CGFloat)alpha
{
    [UIView animateWithDuration:0.2 animations:^{
        [self.navigationController.navigationBar wr_setBarButtonItemsAlpha:alpha hasSystemBackIndicator:NO];
    }];
}

- (void)updateSearchBarColor:(CGFloat)alpha
{
    UIColor *color = [[UIColor colorWithRed:29/255.0 green:160/255.0 blue:126/255.0 alpha:1.0] colorWithAlphaComponent:alpha];
    UIImage *image = [UIImage imageNamed:@"search"];
    image = [image wr_updateImageWithTintColor:color alpha:alpha];
    [self.searchButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
#pragma mark -UICollectionViewDelegate
-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:flowLayout];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //背景颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        //自适应大小
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        //注册
        [_collectionView registerClass:[HomeFirstCollectionViewCell class] forCellWithReuseIdentifier:@"homefirstColl"];
        [_collectionView registerClass:[HomeSecondCollectionViewCell class] forCellWithReuseIdentifier:@"homesecondColl"];
        [_collectionView registerClass:[HomeThirdCollectionViewCell class] forCellWithReuseIdentifier:@"homethirdColl"];
        [_collectionView registerClass:[HomeFourCollectionViewCell class] forCellWithReuseIdentifier:@"homefourColl"];
        [_collectionView registerClass:[HomeFiveCollectionViewCell class] forCellWithReuseIdentifier:@"homefiveColl"];
        [_collectionView registerClass:[HomeSixCollectionViewCell class] forCellWithReuseIdentifier:@"homesixColl"];
        [_collectionView registerClass:[HomeSevenCollectionViewCell class] forCellWithReuseIdentifier:@"homesevenColl"];
        [_collectionView registerClass:[HomeEightCollectionViewCell class] forCellWithReuseIdentifier:@"homeeightColl"];
        [_collectionView registerClass:[HomeNineCollectionViewCell class] forCellWithReuseIdentifier:@"homenineColl"];
        [_collectionView registerClass:[LineGrayCollectionViewCell class] forCellWithReuseIdentifier:@"LineGrayColl"];
        
    }
    return _collectionView;
}
//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HomeFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefirstColl forIndexPath:indexPath];
       
        cell.lab.font = [UIFont fontWithName:@"Marion" size:16.0];
        if (indexPath.item == 0) {
            cell.img.image = [UIImage imageNamed:@"购物"];
            cell.lab.text = @"便捷购物";
        }
        else if (indexPath.item == 1){
            cell.img.image = [UIImage imageNamed:@"超市"];
            cell.lab.text = @"社区超市";
        }
        else if (indexPath.item == 2){
            cell.img.image = [UIImage imageNamed:@"便民服务"];
            cell.lab.text = @"便民服务";
        }
        else{
            cell.img.image = [UIImage imageNamed:@"外卖"];
            cell.lab.text = @"美食外卖";
        }
        
        return cell;
        
    }
    else if (indexPath.section == 1){
        LineGrayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LineGrayColl forIndexPath:indexPath];
        return cell;
        
    }
    else if (indexPath.section == 2){
        HomeSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homesecondColl forIndexPath:indexPath];
        cell.rightImg.image = [UIImage imageNamed:@"快来-入驻"];
        return cell;
    }
    else if (indexPath.section == 3){
        HomeThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homethirdColl forIndexPath:indexPath];
        cell.layer.borderWidth = 0.3;
        cell.layer.borderColor = [UIColor colorWithRed:236.0/255.0 green:236.0/255.0 blue:236.0/255.0 alpha:1.0].CGColor;
        if (indexPath.item == 0) {
            cell.rightImg.image = [UIImage imageNamed:@"专题"];
            cell.bigLab.text = @"专题活动";
            cell.contLab.text = @"品牌上新";
        }
        else if (indexPath.item == 1){
            cell.rightImg.image = [UIImage imageNamed:@"聚餐"];
            cell.bigLab.text = @"年底聚餐";
            cell.contLab.text = @"品牌上新";
        }
        else if (indexPath.item == 2){
            cell.rightImg.image = [UIImage imageNamed:@"秒杀专区"];
            cell.bigLab.text = @"秒杀专区";
            cell.contLab.text = @"品牌上新";
        }
        else{
            cell.rightImg.image = [UIImage imageNamed:@"推荐有奖"];
            cell.bigLab.text = @"推荐有奖";
            cell.contLab.text = @"品牌上新";

        }
        return cell;
    }
    else if (indexPath.section == 4){
        HomeFourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefourColl forIndexPath:indexPath];
        cell.backgroundColor = LineColor;
        cell.advImg.image = [UIImage imageNamed:@"广告"];
        return cell;
    }
    else if (indexPath.section == 5){
        HomeFiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefiveColl forIndexPath:indexPath];
//        cell.advImg.image = [UIImage imageNamed:@"广告"];
        cell.nameLab.text = @"优选商家";
        cell.englishLab.text = @"·business";
        return cell;
    }
    else if (indexPath.section == 6){
        HomeSixCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homesixColl forIndexPath:indexPath];
//        cell.backgroundColor = [UIColor redColor];
        if (indexPath.item == 0) {
            cell.nameLab.text = @"周黑鸭";
            cell.imgView.image = [UIImage imageNamed:@"优选商家"];
        }
        else if (indexPath.item == 1){
            cell.nameLab.text = @"味千拉面";
            cell.imgView.image = [UIImage imageNamed:@"优选商家"];
        }
        else if (indexPath.item == 2){
            cell.nameLab.text = @"亲妈火锅";
            cell.imgView.image = [UIImage imageNamed:@"优选商家"];
        }
        else{
            cell.nameLab.text = @"乐山钵钵鸡";
            cell.imgView.image = [UIImage imageNamed:@"优选商家"];
        }
        return cell;
    }
    else if (indexPath.section == 7){
        HomeFiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefiveColl forIndexPath:indexPath];
        //        cell.advImg.image = [UIImage imageNamed:@"广告"];
        cell.nameLab.text = @"附近超市";
        cell.englishLab.text = @"·supermarket";
        return cell;
    }
    else if (indexPath.section == 8){
        HomeSevenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homesevenColl forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.imageView.image = [UIImage imageNamed:@"附近超市"];
            cell.nameLab.text = @"爱家超市";
        }
        else{
            cell.imageView.image = [UIImage imageNamed:@"附近超市"];
            cell.nameLab.text = @"爱家超市";
        }
        return cell;
    }
    else if (indexPath.section == 9){
        HomeEightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeeightColl forIndexPath:indexPath];
        NSString *words = @"——  猜你喜欢 ——";
        NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Copperplate" size:15.0]}];
        NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        attatch.bounds = CGRectMake(0, 0, 13, 13);
        attatch.image = [UIImage imageNamed:@"猜你喜欢"];
        NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch];
        [strAtt insertAttributedString:string8 atIndex:3];
        cell.label.attributedText = strAtt;
        return cell;
    }
    else{
        HomeNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homenineColl forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:@"猜你喜欢商品图"];
        cell.nameLab.text = @"VANS/烦死男鞋STYLE 36板鞋白蓝白红GD女鞋休闲帆布";
        cell.moneyLab.text = @"￥599";
        cell.numLab.text = @"820人已购买";
        return cell;
    }
}
//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 11;
}
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }
    else if (section == 1){
        return 1;
    }
    else if (section == 2){
        return 1;
    }
    else if (section == 3){
        return 4;
    }
    else if (section == 4){
        return 1;
    }
    else if (section == 5){
        return 1;
    }
    else if (section == 6){
        return 4;
    }
    else if (section == 7){
        return 1;
    }
    else if (section == 8){
        return 2;
    }
    else if (section == 9){
        return 1;
    }
    else{
        return 6;
    }
    
}
//机会选中消息
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  YES;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return (CGSize){MCScreenSize.width/4 , 95};
    }
    else if (indexPath.section == 1){
        return (CGSize){MCScreenSize.width , 5};
    }
    else if (indexPath.section == 2){
        return (CGSize){MCScreenSize.width , 90};
    }
    else if (indexPath.section == 3){
        return (CGSize){MCScreenSize.width/2 , 70};
    }
    else if (indexPath.section == 4){
        return (CGSize){MCScreenSize.width , 90};
    }
    else if (indexPath.section == 5){
        return (CGSize){MCScreenSize.width , 40};
    }
    else if (indexPath.section == 6){
        return (CGSize){MCScreenSize.width/4 , 120};
    }
    else if (indexPath.section == 7){
        return (CGSize){MCScreenSize.width , 40};
    }
    else if (indexPath.section == 8){
        return (CGSize){MCScreenSize.width/2 , 120};
    }
    else if (indexPath.section == 9){
        return (CGSize){MCScreenSize.width , 40};
    }
    else{
        return (CGSize){MCScreenSize.width/2 , 200};
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {//四个主题分类
        
        if (indexPath.item == 0) {//便捷购物
            HomeConvenientViewController *coven = [[HomeConvenientViewController alloc]init];
            [self.navigationController pushViewController:coven animated:YES];
        }
        else if (indexPath.item == 1){//社区超市
            CommunitySupermarketMainViewController *comvc = [[CommunitySupermarketMainViewController alloc]init];
            comvc.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:comvc animated:YES];
        }
        else if (indexPath.item == 2){//便民服务
            ConvenienceOfServiceViewController *convenVC = [[ConvenienceOfServiceViewController alloc]init];
            convenVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:convenVC animated:YES];
        }
        else{//美食外卖
            GourmetDeliveryViewController *gourVC = [[GourmetDeliveryViewController alloc]init];
            gourVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:gourVC animated:YES];
        }
    }
    else if (indexPath.section == 1){

    }
    else if (indexPath.section == 2){
        
    }
    else if (indexPath.section == 3){
        
    }
    else if (indexPath.section == 4){
        
    }
    else if (indexPath.section == 5){
        
    }
    else if (indexPath.section == 6){
        
    }
    else if (indexPath.section == 7){
        
    }
    else if (indexPath.section == 8){
        
    }
    else{
        
    }
}



- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width*2, newSize.height*2));
    [image drawInRect:CGRectMake (0, 0, newSize.width*2, newSize.height*2)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (SDCycleScrollView *)advView
{
    if (_advView == nil) {
        NSArray *localImages = @[@"shouyebanner",@"shouyebanner",@"shouyebanner",@"shouyebanner",@"shouyebanner",@"shouyebanner",];
        _advView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, -IMAGE_HEIGHT, kScreenWidth, IMAGE_HEIGHT) imageNamesGroup:localImages];
        _advView.pageDotColor = [UIColor grayColor];
        _advView.autoScrollTimeInterval = 2;
        _advView.currentPageDotColor = [UIColor whiteColor];
        _advView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    }
    return _advView;
}


- (void)onClickSearchBtn
{}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
